package problem1046;

//1046.最后一块石头的重量
//https://leetcode.cn/problems/last-stone-weight/

import java.util.PriorityQueue;

class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((a, b)->b-a);
        for(int x : stones) queue.add(x);
        while(queue.size() > 1) {
            int y = queue.poll();
            int x = queue.poll();
            if(y > x) queue.add(y-x);
        }
        return queue.size() == 0 ? 0: queue.poll();
    }
}